package csu.big_article.controller;

import csu.big_article.msg.ResponseCode;
import csu.big_article.pojo.CommonResponse;
import csu.big_article.pojo.Result;
import csu.big_article.pojo.User;
import csu.big_article.pojo.UserPwdDTO;
import csu.big_article.service.UserService;
import csu.big_article.util.JwtUtil;
import csu.big_article.util.Md5Util;
import csu.big_article.util.ThreadLocalUtil;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findById")
    public User findUserById(Integer id) {  
        return userService.findUserById(id);
    }

//    @RequestParam @NotBlank(message = "用户名不能为空") @Pattern(regexp = "^\\S{5,16}$") String username,
//    @RequestParam @NotBlank(message = "密码不能为空") @Pattern(regexp = "^\\S{5,16}$") String password
    @PostMapping("/user/register")
    public CommonResponse register(
            @RequestParam String username,
            @RequestParam String password
    ) {
        userService.register(username,password);
        return CommonResponse.createForSuccess(ResponseCode.UserRegisterSuccess.getCode(),ResponseCode.UserRegisterSuccess.getDescription());
    }
    @PostMapping ("/user/login")
    public CommonResponse login(
            @RequestParam String username,
            @RequestParam String password
    ) {
        return CommonResponse.createForSuccess();
    }

    //这里肯定是不对的 业务逻辑应该在service层写怎么在controller里面写呢 我说我怎么没有登录的接口，直接写垃圾代码写在controller完全没有封装
    //而且是使用了findUserByName和getPassword

//    @Pattern(regexp = "^\\S{5,16}$") String username,
//    @Pattern(regexp = "^\\S{5,16}$") String password


//    @PostMapping ("/user/login")
//    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username,
//                                @Pattern(regexp = "^\\S{5,16}$") String password) {
//        User u = userService.findUserByName(username);
//            Map<String,Object> claims = new HashMap<>();
//            claims.put("id",u.getId());
//            claims.put("username",u.getUsername());
//
//            //通过id和username构建token的claims
//            String token = JwtUtil.GenerateToken(claims);
//            //将token放入redis当中，不允许多token登录
//
//
////            template.opsForValue().set(u.getUsername(),token,3, TimeUnit.DAYS);
//            return Result.success(token);
//    }

    @GetMapping("/user/userInfo")
    public Result<User> userInfo() {
        //从ThreadLocal本地线程中得到map资源
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = map.get("username").toString();

        //使用mybatis获取对应信息的用户
        User user = userService.findUserByName(username);
        return Result.success(user);
    }

    //更新用户基本信息（昵称和邮箱）
    @PutMapping("/user/update")                                                                                                                                                                                                     
    public Result update(@RequestBody @Validated User user) {
        //这里其实是可以拿别人的token来改你的信息（maybe，直接使用请求服务器来操作？我觉得可以
        userService.update(user);
        return Result.success();
    }

    @PatchMapping("/user/updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl){
        userService.updateAvatar(avatarUrl);
        return Result.success(avatarUrl);
    }

    @PatchMapping("/user/updatePwd")
    public Result<UserPwdDTO> updatePwd(@RequestBody @Validated UserPwdDTO userPwdDTO) {
        Map<String, Object> mapUtil = ThreadLocalUtil.get();
        String username = mapUtil.get("username").toString();
        User user = userService.findUserByName(username);

        if (user.getPassword().equals(Md5Util.getMD5String(userPwdDTO.getOld_pwd()))) {
            if (Objects.equals(userPwdDTO.getNew_pwd(), userPwdDTO.getRe_pwd())) {
                userService.updatePwd(userPwdDTO.getNew_pwd());
                return Result.success(userPwdDTO);
            } else {
                return Result.error("两次密码不一致");
            }
        } else {
            return Result.error("原密码错误");
        }
    }
}
